package core

import (
	"errors"
	"faker/project-public/core/internal"
	"faker/project-public/global"
	"fmt"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"os"
)

func Zap() (logger *zap.Logger) {
	ok := true
	if ok, _ = PathExists("log"); !ok {
		fmt.Printf("create %v directory\n", "log")
		_ = os.Mkdir("log", os.ModePerm)
	}

	cores := internal.Zap.GetZapCores()
	logger = zap.New(zapcore.NewTee(cores...))
	if global.FAKER_CONFIG.Zap.ShowLine {
		logger = logger.WithOptions(zap.AddCaller())
	}
	return logger
}

// PathExists 如果后面功能遇到这个方法，可以单独拎出来放到utils工具包里面
func PathExists(path string) (bool, error) {
	file, err := os.Stat(path)
	if err == nil {
		//判断是否文件夹
		if file.IsDir() {
			return true, nil
		}
		return false, errors.New("存在同名文件")
	}
	if os.IsExist(err) {
		return false, nil
	}
	return false, nil
}
